package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

/*
    @Transactional是Spring提供的一个用于管理事务的注解。可以管理类或者方法上的事务行为
    在对数据厍做操作时,可以确保方法中的所有数据库操作都在同一事务中执行,要么都成功,要么都失败
 */
@Transactional
@Service
@Slf4j
public class AppilcationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper  userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("新增申请单的业务参数：{}",applicationSaveParam);
        Application application=new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        //新增申请单,初始状态为"已发起"
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        /*遇到的问题:新增申请单对应的审批单数据时,审单里没有申请单id
         *原因:执行insert方法对应的SQL时,并没有把刚刚生成的中请单id回填到application对象中
         * 解决办法:给此SQL上加 useGeneratedKeys="true" keyProperty="id” 属性
         * 效果:JDBC会自动回填中诺单id到application对象的id属件中,再传给审批模块,审批就能拿到中请单id]
         */
        //还需要调用审批业务层,为当前申请单同步新增对应的审批数据
        applicationMapper.insert(application);

        /* 在生成申请后还要为此申请单生成对应的审批单数期 */
        auditService.insertAudit(application);
    }
    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单列表业务参数：{}",applicationQuery);
        List<ApplicationVO> list=applicationMapper.selectApplication(applicationQuery);
        //遍历每一个申请单V0,依次为其补全审批入相关数据
        for(int i=0;i<list.size();i++){
            ApplicationVO applicationVO=list.get(i);
            //自定义给指定申请单V0对象补全审批入数据的方法
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void updateStatus(Long id) {
        log.debug("撤销申请单状态业务参数：{}",id);
        Application application=new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //还需要删除此申请单对应的审批单数据
        auditMapper.deleteAuditByApplicationId(id);
    }

    @Override
    public void distributeVehicle(Long applicationId, Long vehicleId) {
        log.debug("车辆分配，applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application=new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");//1空闲 2占用 3维修 4租赁...
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("还车业务:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application=new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        application.setVehicleId(null);
        applicationMapper.back(application);
    }


    private void assignAuditUserList(ApplicationVO applicationVO) {
        //1.准备空集合,用来存放审批入id与姓名
        List<Long> auditUserIdList=new ArrayList<>();
        List<String> auditUsernameList=new ArrayList<>();
        //2.根据申请单id查审批此申请单的审批集合
        List<AuditVO> auditVOList=auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.依次取出每个审批单
        for(int i=0;i<auditVOList.size();i++){
            AuditVO auditVO=auditVOList.get(i);
            //4.获取当前审批单的审批入id
            Long id=auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.获取审批入姓名 并 存入上方空集合中
            UserVO userVO=userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.准备拼接工具
        StringJoiner stringJoiner=new StringJoiner(",");
        //7.依次将每个审批入姓名装入StringJoiner中
        for(String username:auditUsernameList){
            stringJoiner.add(username);
        }
        //8.给申请单V0对象设置审批入id集合与审批人姓名字(补全数据)
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }

}
